package com.jifeng.br.pjj.bookreader.controller.admin;


import com.jifeng.br.pjj.bookreader.entity.BookEntity;
import com.jifeng.br.pjj.bookreader.service.BookService;
import com.jifeng.br.pjj.bookreader.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 图书表 前端控制器
 * </p>
 *
 * @author 机蜂
 * @since 2023-07-11
 */
@RestController
@RequestMapping("/book")
public class BookController extends BaseController {
    @Autowired
    private BookService bookService;

    @GetMapping("/page")
    public R page(int page, int limit, BookEntity book) {
        if (getUser() != null && getUser().getRole() != 1) {
            book.setAuthorId(getUserId());
        }
        return R.data(bookService.getPage(page, limit, book));
    }

    @GetMapping("/info/{bookId}")
    public R info(@PathVariable int bookId) {
        return R.data(bookService.getById(bookId));
    }

    @PostMapping("/update")
    public R update(@RequestBody BookEntity book) {
        bookService.update(book);
        return R.ok();
    }

    @DeleteMapping("/delete/{bookId}")
    public R delete(@PathVariable int bookId) {
        bookService.delete(bookId);
        return R.ok();
    }
}

